import { createRouter, createWebHistory } from "vue-router"
import { useStore } from "@/store"

import home from "@/view/home.vue"
import login from "@/view/login.vue"
import layout from "@/layout/index.vue"
import {systemRoutes} from "@/router/modeles/systemRoutes"
import {personal} from "@/router/modeles/personal"
// import routess from "~pages";
const whileList = [
	""
]
const routes = [
	{
		path: '/',
		redirect: '/dashboard',
		component: layout,
		name: 'home',
		children:systemRoutes
	},
	{
		path: '/home',
		component: layout ,
		name: 'a',
	},
	// ...systemRoutes,
	...personal,
	{
		path: '/login',
		component: login,
		name: 'login',
		meta: {
			title:"登录",
			breadcrumb: false
		}
	},
	// ...routess,
	{ path: "/:catchAll(.*)", redirect: '/404' },

]
export const router = createRouter({
	history: createWebHistory(),
	routes
})

router.beforeEach(async (to, from, next) => {
	// console.log("1:", 1);
	// console.log("to:", to);
	const store = useStore()
	store.isLoadingStart()
	// console.log("store.isLogin:", store.isLogin);
	if (store.isLogin) {
		if (to.name !== "login") {
			// console.log("不是login页登录");
			next()
		} else {
			// console.log("是login页登录跳到首页");
			next({
				name: "home",
				replace: true
			})
		}
	} else {
		if (to.name === "login") {
			// console.log("没登录是login页");
			next()
		} else {
			// console.log("没登录不是login页跳到login");
			next({
				name: "login",
				replace: true
			})
		}
	}

})
router.afterEach((to, from) => {
	const store = useStore()
	store.isLoadingEnd()
})